package com.lcm.openapi.common.models.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.data.factory.core.util.DataUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Objects;

/**
 * @author : lcm
 * @date : 2024/07/18
 **/
@Data
public class PageVo<T> {

    @ApiModelProperty("当前页")
    private Long current;

    @ApiModelProperty("每页显示数量")
    private Long size;

    @ApiModelProperty("总数量")
    private Long total;

    @ApiModelProperty("当前页数据")
    private List<T> records;

    public static <T ,R> PageVo<R> casePageVO(Page<T> pageList , Class<R> cls){
        if(Objects.nonNull(pageList)) {
            List<T> records = pageList.getRecords();
            if (CollectionUtil.isNotEmpty(records)) {
                PageVo<R> pageVO = new PageVo<>();
                BeanUtil.copyProperties(pageList, pageVO);
                List<R> voRecords = Convert.toList(cls, records);
                pageVO.setRecords(voRecords);
                return pageVO;
            }
        }
        return null;
    }

    public static <T> PageVo<T> getMockPageList(Class<T> cls){
        PageVo<T> pageVo = new PageVo<>();
        pageVo.setCurrent(1L);
        pageVo.setSize(1L);
        pageVo.setTotal(1L);
        pageVo.setRecords(ListUtil.of(DataUtil.build(cls)));
        return pageVo;
    }

}
